package com.yhd.tomcat.req;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

/**
 * @author yhd
 * @email yinhuidong1@xiaomi.com
 * @description TODO
 * @since 2021/6/10 下午5:52
 */
public class HttpRequest extends BaseRequest{
    public HttpRequest(InputStream in) {
        super(in);
    }

    @Override
    protected void doProcess() {
        try {
            String context = "";
            byte[] buffer = new byte[1024];
            int len = 0;

            if ((len = in.read(buffer)) > 0) {
                context=new String(buffer,0,len);
            }
            String line =context.split("\\n")[0];
            String[]arr=line.split("\\s");
            setMethod(arr[0]);
            setUrl(arr[1].split("\\?")[0]);

            System.out.println(context);
            String[] strings = context.split("Cookie:");
            String k = "cookie";
            String v= strings[1];
            if (SESSION.containsKey(v))
                cookie= SESSION.get(v);
            else{
                cookie=new HashMap<>();
                cookie.put(k,v);
                SESSION.put(v,cookie);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
